﻿using OnlineMealOrdering.Common;
using OnlineMealOrdering.Domain.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineMealOrdering.Domain
{
    /// <summary>
    /// 订单信息实体
    /// </summary>
    public class OrderEntity : AuditableEntity
    {
        public string Code { get; set; }

        //店铺信息
        public virtual StoreOnlineEntity Store { get; set; }

        public virtual ICollection<ShopItemEntity> Details { get; set; }

        public decimal TotalPrice
        {
            get
            {
                return CalculateTotalPrice();
            }
        }

        public virtual UserEntity User { get; set; }

        public virtual AddressEntity Address { get; set; }

        public EnumOrderStatus Status { get; set; }

        public string Note { get; set; }

        public virtual StoreCommentEntity Comment { get; set; }

        //折扣券
        public virtual CouponEntity Coupon { get; set; }

        public decimal CalculateTotalPrice()
        {
            if (Details.Count == null) return 0m;
            return Details.Sum(a => a.Price);
        }

        public static OrderEntity Create(int storeId, string userCode, string address, string contact, string telphone, string note, string couponCode, List<ShopItemEntity> items)
        {
            var store = DomainContext.Current.EntitySet<StoreOnlineEntity>().SingleOrDefault(a => a.Id == storeId);
            if (store == null)
                BusinessException.Create("找不到店铺ID:{0}", storeId);

            var user = DomainContext.Current.EntitySet<UserEntity>().SingleOrDefault(a => a.UserCode == userCode);
            if (user == null)
                BusinessException.Create("找不到用户ID:{0}", storeId);

            var addressEntity = AddressEntity.Create(address, contact, telphone);

            var coupon = DomainContext.Current.EntitySet<CouponEntity>().Single(a => a.Code == couponCode);
            if(coupon==null)
                BusinessException.Create("找不到购物券Code:{0}", storeId);
            if(coupon.Status!=EnumCouponStatus.Enable)
                BusinessException.Create("购物券无效，Code:{0}", storeId);

            //设置购物券为使用状态
            coupon.Status = EnumCouponStatus.Apply;

            var entity = new OrderEntity();
            entity.Code = Guid.NewGuid().ToString().Replace("-","");
            entity.Address = addressEntity;
            entity.Store = store;
            entity.User = user;
            entity.Note = note;
            entity.Status = EnumOrderStatus.Create;
            entity.Coupon = coupon;
            entity.Details = items;
            
            return entity;
        }

        public void ChangeStatus(EnumOrderStatus status)
        {
            //此处要判断订单状态
            Status = status;

            //如果更改订单状态 需要发送手机短信

        }

        public void AddComment(string content, int? refid)
        { 
            
        }


    }
}
